package com.kh.first;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.kh.first.pojo.ReportDetailPojo;
import com.kh.first.utils.Len;

public class SqlGen {

    public static void main(String[] args) throws Exception {
        Class<?> clazz = ReportDetailPojo.class;
        String tableName = "report_detail";
//        String string = genTable(clazz, tableName);
//        System.out.println(string);
        genMapper(clazz, tableName);
    }

    private static void genMapper(Class<?> clazz, String tableName) throws Exception {
        Map<String, Object> root = new LinkedHashMap<String, Object>();
        String simpleName = clazz.getSimpleName();
        String packageName = "com.kh.first.dao";// clazz.getPackage().toString();
        String beanName = simpleName.replace("Pojo", "");
        String pri = "id";
//        String pri = "userid";
        String daoAllName = packageName + "." + (beanName) + "Dao";
        root.put("daoAllName", daoAllName);
        root.put("packageName", packageName);
        root.put("beanName", beanName);
        root.put("tableName", tableName);
        root.put("beanAllName", clazz.getName());
        root.put("pri", pri);
        Field[] fields = clazz.getDeclaredFields();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (Field field : fields) {
            String name = field.getName();
            Class<?> type = field.getType();
            Class<?> clazzInt = Integer.class;
            Class<?> clazzLong = Long.class;
            Class<?> clazzDate = Date.class;
            Class<?> clazzDecimal = BigDecimal.class;
//			Class<?> clazzString = String.class;
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("COLUMN_NAME", name);

            map.put("DATA_TYPE", "varchar");
            if (clazzDate == type)
                map.put("DATA_TYPE", "date");
            if (clazzDecimal == type)
                map.put("DATA_TYPE", "decimal");
            if (clazzInt == type || clazzLong == type)
                map.put("DATA_TYPE", "int");
            list.add(map);
        }
        root.put("columns", list);
        root.put("pri_type", "int");
        String fprint = FreemarkerTest.fprint("mapper.ftl", root);
        System.out.println(fprint);
    }

    /**
     * 首字母转小写
     *
     * @param s
     * @return
     */
    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    private static String genTable(Class<?> clazz, String tableName) {
        Field[] fields = clazz.getDeclaredFields();
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("drop table if exists %s;\n", tableName));
        sb.append(String.format("create table `%s` (\n", tableName));
//        sb.append("userid varchar(40) auto_increment,\n");
        for (Field field : fields) {
            String name = field.getName();
            Class<?> type = field.getType();
            Len len = field.getAnnotation(Len.class);
            Class<?> clazzInt = Integer.class;
            Class<?> clazzDate = Date.class;
            Class<?> clazzString = String.class;
            Class<?> clazzLong = Long.class;
            Class<?> clazzArray = List.class;
            int fLength = 0;// 字符串的长度
            String sqlFieldType = "";
            if (null != len) {// 有长度的字符串和整数怎么办？
                fLength = len.value();
            } else {
                if (type == clazzInt) {
                    fLength = 3;
//					sqlFieldType=" int("
                }
            }
            if (type == clazzInt) {
                fLength = 2;
                sb.append(name + " int(3)");
            } else if (type == clazzString) {
                sb.append(name + " varchar(255)");
            } else if (type == clazzDate) {
                sb.append(name + " datetime");
            } else if (type == clazzLong) {
                sb.append(name + " bigint(20)");
            } else if (type == clazzArray){
                sb.append(name + " varchar(255)");
            }
            sb.append(",\n");
        }

        sb.append("primary key (`id`)\n" + ")");
        String string = sb.toString();
        return string;
    }
    private static String genTable1(Class<?> clazz, String tableName) {
        Field[] fields = clazz.getDeclaredFields();
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("drop table if exists %s;\n", tableName));
        sb.append(String.format("create table `%s` (\n", tableName));
//        sb.append("userid varchar(40) auto_increment,\n");
        for (Field field : fields) {
            String name = field.getName();
            Class<?> type = field.getType();
            Len len = field.getAnnotation(Len.class);
            Class<?> clazzInt = int.class;
            Class<?> clazzDate = Date.class;
            Class<?> clazzString = String.class;
            Class<?> clazzLong = Long.class;
            Class<?> clazzArray = List.class;
            int fLength = 0;// 字符串的长度
            String sqlFieldType = "";
            if (null != len) {// 有长度的字符串和整数怎么办？
                fLength = len.value();
            } else {
                if (type == clazzInt) {
                    fLength = 3;
//					sqlFieldType=" int("
                }
            }
            if (type == clazzInt) {
                fLength = 2;
                sb.append(name + " int(3)");
            } else if (type == clazzString) {
                sb.append(name + " varchar(255)");
            } else if (type == clazzDate) {
                sb.append(name + " datetime");
            } else if (type == clazzLong) {
                sb.append(name + " bigint(20)");
            } else if (type == clazzArray){
                sb.append(name + " varchar(255)");
            }
            sb.append(",\n");
        }

        sb.append("primary key (`id`)\n" + ")");
        String string = sb.toString();
        return string;
    }
}
